package io.renren.modules.work.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.ConfigConstant;
import io.renren.common.utils.Query;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.work.dto.FindProjectDTO;
import io.renren.modules.work.entity.ScCompanyEntity;
import io.renren.modules.work.entity.ScExtrasEntity;
import io.renren.modules.work.entity.ScProjectEntity;
import io.renren.modules.work.entity.ScProjectdtlEntity;
import io.renren.modules.work.service.ScExtrasService;
import io.renren.modules.work.service.ScProjectService;
import io.renren.modules.work.service.ScProjectdtlService;
import io.renren.vo.ScProjectEntityVo;
import io.swagger.annotations.Api;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 工程
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-03-24 23:31:29
 */
@RestController
@RequestMapping("work/scproject")
@Api("工程测试接口")
public class ScProjectController extends AbstractController {
    @Autowired
    private ScProjectService scProjectService;
    @Autowired
    private ScProjectdtlService scProjectdtlService;
    @Autowired
    private ScExtrasService scExtrasService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:scproject:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scProjectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{projectid}")
    @RequiresPermissions("work:scproject:info")
    public R info(@PathVariable("projectid") String projectid) throws InvocationTargetException, IllegalAccessException {
        ScProjectEntity scProjectEntity = scProjectService.getById(projectid);
        ScProjectEntityVo scProjectEntityVo = new ScProjectEntityVo();
        BeanUtils.copyProperties(scProjectEntityVo,scProjectEntity);
        QueryWrapper<ScExtrasEntity> scExtrasEntityQueryWrapper = new QueryWrapper<>();
        scExtrasEntityQueryWrapper.eq("projectid",projectid);
        ScExtrasEntity scExtrasEntity = scExtrasService.getOne(scExtrasEntityQueryWrapper);
        if(scExtrasEntity!=null){
            scProjectEntityVo.setExtrasPrice(scExtrasEntity.getPrice()==null?0:scExtrasEntity.getPrice().doubleValue());
        }
        return R.ok().put("scProject", scProjectEntityVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("work:scproject:save")
    public R save( @RequestBody ScProjectEntity scProject){
        SysUserEntity user = getUser();
        scProject.setCremanid(Integer.parseInt(user.getUserId().toString()));
        scProject.setCremanname(user.getUsername());
        scProject.setProjectStatus(ConfigConstant.PROJETCT_INITIAL);
        scProjectService.saveProject(scProject);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update1")
    @RequiresPermissions("work:scproject:update")
    public R update(@RequestBody ScProjectEntity scProject){
		scProjectService.updateById(scProject);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @Transactional
    public R delete(@RequestBody String[] projectids){
		scProjectService.removeByIds(Arrays.asList(projectids));

        for (String projectid : projectids) {
            QueryWrapper<ScProjectEntity> scProjectEntityQueryWrapper = new QueryWrapper<>();
            scProjectEntityQueryWrapper.eq("projectid",projectid);
            scProjectService.remove(scProjectEntityQueryWrapper);


            QueryWrapper<ScProjectdtlEntity> scProjectdtlEntityQueryWrapper = new QueryWrapper<>();
            scProjectdtlEntityQueryWrapper.eq("projectid",projectid);
            scProjectdtlService.remove(scProjectdtlEntityQueryWrapper);
        }
        return R.ok();
    }


    /**
     * 提交保存工程表单
     */
    @RequestMapping("/commit")
    public R commitProject(@RequestBody String params){
        SysUserEntity user = getUser();

        return scProjectService.commitProject(params,user);
    }

    /**
     * 提交更新工程表单
     */
    @RequestMapping("/update")
    public R updateProject(@RequestBody String params){
        return scProjectService.updateProject(params);
    }

    /**
     * 验收确认
     */
    @RequestMapping("/chk")
    public R chkProject(@RequestBody String[] projectdtlids){
        return scProjectService.chkProject(projectdtlids);
    }
    /**
     * 完工
     */
    @RequestMapping("/finish")
    public R finishProject(@RequestBody String[] projectids){
        return scProjectService.finishProject(projectids);
    }


    /**
     * 查询子工程（APP）弃用
     */
    @RequestMapping("/findByShipNo")
    public R findByShipNo2(@RequestParam("shipNo") String shipNo){
        QueryWrapper<ScProjectEntity> scProjectEntityQueryWrapper = new QueryWrapper<>();
        scProjectEntityQueryWrapper.eq("shipsideno",shipNo);
        ScProjectEntity one = scProjectService.getOne(scProjectEntityQueryWrapper);
        if(one==null){
            return R.error("未查询到该记录");
        }
        QueryWrapper<ScProjectdtlEntity> scProjectdtlEntityQueryWrapper = new QueryWrapper<>();
        scProjectdtlEntityQueryWrapper.eq("projectid",one.getProjectid());
        List<ScProjectdtlEntity> list = scProjectdtlService.list(scProjectdtlEntityQueryWrapper);
        return R.ok().put("data",list);
    }

    /**
     * 查询子工程（APP）
     */
    @RequestMapping("/findProjectDtl")
    public R findByShipNo(@RequestBody FindProjectDTO findProjectDTO){

        QueryWrapper<ScProjectdtlEntity> projectdtlEntityQueryWrapper = new QueryWrapper<>();

        //IPage<ScProjectdtlEntity> page = scProjectdtlService.page(new Query<ScProjectdtlEntity>().getPage(null), projectdtlEntityQueryWrapper);
        return R.ok();
    }


    /**
     *  获取所有船号（不重复）
     */
    @RequestMapping("getAllShipSideNo")
    public R getAllShipSideNo(@RequestParam("shipSideNo") String shipSideNo){
        return scProjectService.getAllShipSideNo(shipSideNo);

    }
    /**
     * 确认出库
     */
    @PostMapping("outConfirm")
    public R outConfirm(@RequestBody String consumablesid){
        return scProjectService.outConfirm(consumablesid);
    }

}
